package com.bootdo.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.system.domain.ProductConfigDO;
import com.bootdo.system.service.ProductConfigService;
import com.bootdo.system.vo.ListWithOrderQueryVo;
import com.bootdo.system.vo.ListWithOrderVo;
import net.sf.json.JSONArray;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品配置表
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2021-06-15 13:40:52
 */
 
@Controller
@RequestMapping("/system/config")
public class ProductConfigController {
	@Autowired
	private ProductConfigService productConfigService;
	
	@GetMapping()
	@RequiresPermissions("system:config:config")
	String Config(){
	    return "system/config/config";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:config:config")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ProductConfigDO> configList = productConfigService.list(query);
		int total = productConfigService.count(query);
		PageUtils pageUtils = new PageUtils(configList, total);
		return pageUtils;
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:config:add")
	public R save( ProductConfigDO config){
		if(productConfigService.save(config)>0){
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:config:remove")
	public R remove(Integer id){
		if(productConfigService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	@Log("导入")
	@PostMapping(value = "/import")
	@ResponseBody
	public R importInfo(@RequestParam("file") MultipartFile file, HttpServletRequest req, HttpServletResponse resp) throws Exception {
		try {
			productConfigService.importInfo(file);
			return R.ok();
		} catch (Exception e) {
			return R.error(e.getMessage());
		}
	}

	@GetMapping("/getWithOrderList/{type}")
	String getWithOrderList(@PathVariable("type") String type){
		return "system/config/" + type;
	}

	/**
	 * 查询随货同行单列表
	 * @param params
	 * @return
	 */
	@ResponseBody
	@GetMapping("/getListWithOrder")
	public PageUtils getListWithOrder(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<ListWithOrderVo> listWithOrderVos = productConfigService.getListWithOrder(query);
		int total = productConfigService.countListWithOrder(query);
		PageUtils pageUtils = new PageUtils(listWithOrderVos, total);
		return pageUtils;
	}


	@GetMapping("/parameterSetting/{type}")
	String parameterSetting(@PathVariable("type") String type, @RequestParam("freightTime") String freightTime,
			   @RequestParam("expressNos") String[] expressNos, Model model) {
		model.addAttribute("type", type);
		model.addAttribute("freightTime", freightTime);

		List<String> expressNoList = Arrays.asList(expressNos);
		JSONArray json = JSONArray.fromObject(expressNoList);
		model.addAttribute("expressNos", json);
		return "system/config/parameterSetting";
	}

	@ResponseBody
	@RequestMapping("/batchPrint")
	public R batchPrint(ListWithOrderQueryVo listWithOrderQueryVo) {
		List<JSONObject> printList = productConfigService.batchPrint(listWithOrderQueryVo);
		Map<String, Object> map = new HashMap<>();
		map.put("printList",printList);
		return R.ok(map);
	}


	
}
